﻿Imports System.Data
Imports Global_Visas_Business_Layer
Partial Public Class frmLogin

    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOk.Click
        Dim dt As DataTable
        Try
            If UsernameTextBox.Text.Trim <> String.Empty And PasswordTextBox.Password.Trim <> String.Empty Then
                dt = CommonDataFunctions.VerifyConsultantDetails(UsernameTextBox.Text.ToString)
                If Not dt Is Nothing Then
                    If dt.Rows.Count > 0 Then
                        If dt.Rows.Count = 1 Then
                            If dt.Rows(0).ItemArray(1).ToString.Trim = PasswordTextBox.Password.ToString.Trim Then
                                Dim test As Singleton = Singleton.Instance
                                test.CurrentConId = Convert.ToInt32(dt.Rows(0).ItemArray(2).ToString)
                                test.CurrentConName = dt.Rows(0).ItemArray(0).ToString.Trim
                                test.CurrentConEmail = dt.Rows(0).ItemArray(3).ToString()
                                test.CurrentConEmailPassword = dt.Rows(0).ItemArray(4).ToString()
                                test.CurrentConSecurityLevel = Convert.ToInt32(dt.Rows(0).ItemArray(5).ToString())
                                GlobalCRM.CurrentConName = dt.Rows(0).ItemArray(0).ToString.Trim
                                GlobalCRM.CurrentConID = Convert.ToInt32(dt.Rows(0).ItemArray(2).ToString)
                                Dim frmCRMMDI1 As frmCRMMDI1 = New frmCRMMDI1
                                Me.Hide()
                                frmCRMMDI1.ShowDialog()
                            Else
                                GlobalCRM.CurrentConName = String.Empty
                                MsgBox("Incorrect Password", MsgBoxStyle.Information, "CRM")
                                PasswordTextBox.Password = ""
                            End If
                        ElseIf dt.Rows.Count > 1 Then
                            MsgBox("There are more than 1 account been set up with your login credentials. Please contact IT to sort out this discrepancy", MsgBoxStyle.Information, "CRM V3")
                            Me.Close()
                            Exit Sub
                        End If
                    Else
                        MsgBox("Username not been set up on the CRM system. Please contact IT", MsgBoxStyle.Information, "CRM")
                        UsernameTextBox.Text = ""
                        PasswordTextBox.Password = ""
                    End If
                Else
                    MsgBox("Username not been set up on the CRM system. Please contact IT", MsgBoxStyle.Information, "CRM")
                    UsernameTextBox.Text = ""
                    PasswordTextBox.Password = ""
                End If

            Else
                MsgBox("Username and password fields are mandatory.", MsgBoxStyle.Information, "CRM")
                UsernameTextBox.Text = ""
                PasswordTextBox.Password = ""
            End If

        Catch ex As Exception
        Finally
            dt = Nothing
            'Me.Close()
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        End
    End Sub

    Private Sub frmLogin_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.UsernameTextBox.Focus()
        
        'Dim frmCRMMDI1 As frmCRMMDI1 = New frmCRMMDI1
        'Me.Hide()
        'frmCRMMDI1.ShowDialog()
    End Sub
End Class
